﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Core;
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Silverlight.UI;
using NLog;

namespace Agility.TestProject.WebAii
{
    public class WebAiiDriver : IDisposable
    {
        Manager telerikManager;
        Settings telerikSettings;

        public SilverlightApp App { get; private set; }

        Logger logger = LogManager.GetCurrentClassLogger();

        public WebAiiDriver()
        {
            logger.Debug("--> Entering constructor");
            this.telerikSettings = new Settings();
            this.telerikSettings.Web.DefaultBrowser = BrowserType.InternetExplorer;
            logger.Debug("Browsertype is InternetExplorer");
            this.telerikSettings.Web.EnableSilverlight = true;
            logger.Debug("Enabled Silverlight integration");
            this.telerikSettings.Web.UseHttpProxy = true;
            logger.Debug("Using default HTTP proxy");
            this.telerikSettings.Web.SilverlightConnectTimeout = 30000;
            logger.Debug("<-- Leaving constructor");
        }


        public void Initialize()
        {
            logger.Debug("--> Entering Initialize");
            this.telerikManager = new Manager(this.telerikSettings);
            this.telerikManager.Start();
            this.telerikManager.LaunchNewBrowser();
            string url = "http://localhost:52878/Agility.aspx";
            this.telerikManager.ActiveBrowser.NavigateTo(url);
            logger.Debug("Opened {0}", url);
            this.App = this.telerikManager.ActiveBrowser.SilverlightApps()[0];
            logger.Debug("<-- Leaving Initialize");
        }

        public void Cleanup()
        {
            logger.Debug("--> Entering Cleanup");
            this.Dispose(true);
            GC.SuppressFinalize(this);
            logger.Debug("<-- Leaving Cleanup");
        }

        public bool ConfirmInitialize()
        {
            logger.Debug("--> Entering ConfirmInitialize");
            bool result = false;
            if (this.App != null)
            {
                result = true;
            }
            logger.Debug("<-- Leaving ConfirmInitialize");
            return result;
        }


        #region Disposable implementation
        bool disposed;

        public void Dispose()
        {
            this.Cleanup();
        }
        
         ~WebAiiDriver()
        {
            this.Dispose(false);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // release all managed resources here
                    if (!this.telerikManager.Disposed)
                    {
                        this.telerikManager.Dispose();
                    }

                }
                // release any unmanaged resources here

                this.disposed = true;
            }
        }
        #endregion
        

    }
}
